#!/bin/bash
mount /dev/drbd1 /var/opt/gitlab/git-data
drbdadm primary r000
chown -R git:root /var/opt/gitlab/git-data
b=' '
echo 'b:'$b
while [[ $b == ' ' ]]
do
 su - gitlab-psql <<'EOF'
 psql -h 192.168.1.140 -d postgres <<'EOF'
 SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname='gitlabhq_production' AND pid<>pg_backend_pid();
 \q
 exit
EOF
 a=`su - gitlab-psql <<'EOF'
 psql -h 192.168.1.140 -d postgres <<'EOF'
 SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname='gitlabhq_production' AND pid<>pg_backend_pid();
 \q
 exit
EOF`
 b=`echo $a | grep "(0 rows)"`
done
su - gitlab-psql <<EOF
psql -h 192.168.1.140 -d postgres <<EOF
drop database gitlabhq_production;
create database gitlabhq_production;
\q
exit
EOF
su - gitlab-psql <<'EOF'
c=`find /var/opt/gitlab/postgresql -mtime -1 -name '*.sql' -type f | awk -F '/' '{print $6}'`
echo $c
psql -h 192.168.1.140 -d gitlabhq_production -f $c >/dev/null 2>&1 
EOF

